const list=JSON.parse(window.localStorage.getItem("detailList"))
const loginName=window.sessionStorage.getItem("loginName")
new Vue({
    el:'#i_bg',
    data:{
        list:[],
        totalPrice:0,
        imgStr:'http://192.168.2.106:8848/images/',
        addressList:[],
        address:{

        }
    },
    methods:{
      getAddressList()
      {
          axios.get("/api/address/getAddressList",
              {params: {
                  loginName
                  }
              }).then(rs=>{
                  this.addressList=rs.data.addressList
                  this.addressList.forEach((addr,index)=>{
                      if(addr.isDefault===1)
                      {
                          this.address=addr
                      }
                  })
          })
      },
        getAddress(id)
        {
            this.addressList.forEach((addr,index)=>{
                if(addr.id===id)
                {
                    this.address=addr
                    this.address.loginName=loginName
                }
            })
        },
        sureOrder()
        {
            if(confirm("确认生成订单吗"))
            {
                const formData=new FormData()
                formData.append("list",JSON.stringify(this.list))
                formData.append("loginName",loginName)
                formData.append("address",JSON.stringify(this.address))
                formData.append("totalPrice",this.totalPrice)
                axios.post("/api/car/generateOrder",
                    formData
                ).then(rs=>{
                    if(rs.data.isGene==="yes")
                    {
                        // let order={id:rs.data.orderId,totalPrice:this.totalPrice}
                        window.localStorage.clear()
                        window.localStorage.setItem("order",JSON.stringify(rs.data.order));
                        window.location="/api/BuyCar_Three.html"
                    }
                })
            }

        }
    },
    created()
    {
        this.list=list
        list.forEach((detail,index)=>{
            this.totalPrice+=detail.numPrice
        })
        this.getAddressList()
    }
})